<?php

function _demographic_info($form, &$form_state = NULL) {

    $logged_in_user = getCurrentUser();
    //p($logged_in_user->uid);
    $user_table = db_select('users', 'u')
            ->fields('u')
            ->condition('uid', $logged_in_user->uid, '=')
            ->execute()
            ->fetchAssoc();
    //  p($user_table);
    $la_user_table = db_select('la_users', 'lu')
            ->fields('lu')
            ->condition('user_id', $logged_in_user->uid, '=')
            ->execute()
            ->fetchAssoc();
    // p($la_user_table);
    $form = array();
    $form['title'] = array(
        '#markup' => t("<h2>Demographic Information</h2>"));
    $form_key = "em_parents";

    $form['first_name'] = array(
        '#type' => 'textfield',
        '#title' => t('Legal First Name'),
        '#default_value' => $la_user_table['first_name'],
        '#required' => TRUE,
    );
    $form['middle_name'] = array(
        '#type' => 'textfield',
        '#title' => t('Legal Middle Name'),
        '#default_value' => $la_user_table['middle_name'],
        '#required' => FALSE,
    );
    $form['last_name'] = array(
        '#type' => 'textfield',
        '#title' => t('Legal Last Name'),
        '#default_value' => $la_user_table['last_name'],
        '#required' => TRUE,
    );
    $form['aka_first_name'] = array(
        '#type' => 'textfield',
        '#title' => t('AKA First Name'),
        '#default_value' => $la_user_table['aka_first_name'],
        '#required' => FALSE,
    );
    $form['aka_last_name'] = array(
        '#type' => 'textfield',
        '#title' => t('AKA Last Name'),
        '#default_value' => $la_user_table['aka_last_name'],
        '#required' => FALSE,
    );
    $form['username'] = array(
        '#type' => 'textfield',
        '#title' => t('Username'),
        '#default_value' => $user_table['name'],
        '#required' => TRUE,
    );
    $form['email'] = array(
        '#type' => 'textfield',
        '#title' => t('Email'),
        '#default_value' => $user_table['mail'],
        '#required' => TRUE,
    );

    $startYear = date('Y') - 8;
    $form['dob'] = array(
        '#type' => 'date',
        '#title' => t('DOB'),
        '#start_year' => $startYear,
        '#required' => TRUE,
        '#default_value' => $la_user_table['dob'],
        '#process' => array('form_process_date', 'fix_em_dob_range'),
    );
    $form['cell_phone'] = array(
        '#type' => 'textfield',
        '#title' => t('Phone # 1'),
        '#default_value' => $la_user_table['cell_phone'],
        '#required' => TRUE,
    );
    $form['phone_number'] = array(
        '#type' => 'textfield',
        '#title' => t('Phone # 2'),
        '#default_value' => $la_user_table['phone_number'],
        '#required' => TRUE,
    );
    $form['marital_status'] = array(
        '#type' => 'select',
        '#options' => array(
            "" => t('Select'),
            1 => t('Single'),
            2 => t('Married'),
        ),
        '#title' => t('Marital Status'),
        '#default_value' => $la_user_table['marital_status'],
        '#required' => TRUE,
    );
    $form['gender'] = array(
        '#type' => 'select',
        '#title' => t('Gender'),
        '#options' => array(
            "" => t('Select'),
            1 => t('Male'),
            2 => t('Female'),
        ),
        '#default_value' => $la_user_table['gender'],
        '#required' => TRUE,
    );
    $form['ethnicity'] = array(
        '#type' => 'select',
        '#title' => t('Ethnicity'),
        '#options' => returnEthnicity(),
        '#default_value' => $la_user_table['ethnicity'],
        '#required' => TRUE,
    );
    $form['alternate_contact'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Alternate Method(s) of Contact (other than email)'),
        '#options' => returnContactMethods(),
        '#default_value' => array(),
        '#required' => TRUE,
    );
    $form['text_carrier'] = array(
        '#type' => 'select',
        '#title' => t('Add Carrier for Text'),
        '#options' => drupal_map_assoc(array(t('Carrier 1'), t('Carrier 2'))),
        //'#default_value' => $la_user_table['text_carrier'],
        '#required' => FALSE,
    );
    $form['ca_resident'] = array(
        '#type' => 'radios',
        '#title' => t('CA Resident?'),
        '#options' => array(0 => t('No'), 1 => t('Yes')),
        '#default_value' => $la_user_table['ca_resident'],
        '#required' => TRUE,
    );

    $form[$form_key] = array(
        '#type' => 'container',
        '#tree' => TRUE,
        '#prefix' => '<div id="' . $form_key . '">',
        '#suffix' => '</div>',
    );

    $form['save_draft'] = array(
        '#type' => 'submit',
        '#value' => t('Save/Edit'),
        '#submit' => array('_register_em_demographic_info_submit'),
    );
    return $form;
}

function _addresses($form, &$form_state = NULL) {
    $form = array();
    $form['title'] = array(
        '#markup' => t("<h2>Address</h2>"));

    $states = getStates();

    $form_key = "em_addresses";

    //Set count of fields to show
    $form_state['storage'][$form_key] = isset($form_state['storage'][$form_key]) ? $form_state['storage'][$form_key] : 0;
    if (isset($form_state['triggering_element']) && $form_state['triggering_element']['#name'] == "Remove Address" && $form_state['storage'][$form_key] > 0) {
        $form_state['storage'][$form_key] --;
    } else {
        $form_state['storage'][$form_key] ++;
        if ($form_state['storage'][$form_key] > 2) {
            $form_state['storage'][$form_key] = 2;
        }
    }

    $form[$form_key] = array(
        '#type' => 'container',
        '#tree' => TRUE,
        '#prefix' => '<div id="' . $form_key . '">',
        '#suffix' => '</div>',
    );

    for ($i = 0; $i < $form_state['storage'][$form_key]; $i++) {
        $form[$form_key][$i] = array(
            '#type' => 'fieldset',
            '#tree' => TRUE,
        );

        $form[$form_key][$i]['mailing_address'] = array(
            '#type' => 'textfield',
            '#title' => t(($i == 0) ? 'Primary Mailing Address' : 'Secondary Mailing Address'),
            '#default_value' => '',
            '#required' => TRUE,
        );
        $form[$form_key][$i]['apt_no'] = array(
            '#type' => 'textfield',
            '#title' => t('APT #'),
            '#default_value' => '',
            '#required' => FALSE,
        );
        $form[$form_key][$i]['city'] = array(
            '#type' => 'textfield',
            '#title' => t('City'),
            '#default_value' => '',
            '#required' => TRUE,
        );

        $form[$form_key][$i]['state'] = array(
            '#type' => 'select',
            '#title' => t('State'),
            '#options' => $states,
            '#default_value' => 'CA',
            '#required' => TRUE,
        );
        $form[$form_key][$i]['zip'] = array(
            '#type' => 'textfield',
            '#title' => t('Zip'),
            '#default_value' => '',
            '#required' => TRUE,
            '#element_validate' => array('element_validate_integer_positive'),
        );
        $form[$form_key][$i]['no_of_adults_address'] = array(
            '#type' => 'textfield',
            '#title' => t('# of adults at this address'),
            '#default_value' => '',
            '#element_validate' => array('element_validate_number'),
            '#required' => TRUE,
        );
        $form[$form_key][$i]['no_of_children_address'] = array(
            '#type' => 'textfield',
            '#title' => t('# of children at this address'),
            '#default_value' => '',
            '#element_validate' => array('element_validate_number'),
            '#required' => TRUE,
        );
        $form[$form_key][$i]['mail_at_address'] = array(
            '#type' => 'radios',
            '#title' => t('Do you want mailings at this address'),
            '#options' => array(0 => t('No'), 1 => t('Yes')),
            '#default_value' => array(),
            '#required' => TRUE,
        );
    }
    if ($form_state['storage'][$form_key] < 2) {
        $form[$form_key]['add_address'] = array(
            '#type' => 'button',
            '#href' => '',
            '#title' => t('Add Address'),
            '#default_value' => t('Add Secondary Address'),
            '#name' => t('Add Address'),
            '#limit_validation_errors' => array(),
            '#executes_submit_callback' => FALSE,
            '#ajax' => array(
                'callback' => '_load_addresses',
                'wrapper' => $form_key,
                'effect' => 'fade',
            ),
        );
    }
    if ($form_state['storage'][$form_key] > 1) {
        $form[$form_key]['remove_address'] = array(
            '#type' => 'button',
            '#href' => '',
            '#title' => t('Remove Address'),
            '#default_value' => t('Remove Secondary Address'),
            '#name' => t('Remove Address'),
            '#limit_validation_errors' => array(),
            '#executes_submit_callback' => FALSE,
            '#ajax' => array(
                'callback' => '_load_addresses',
                'wrapper' => $form_key,
                'effect' => 'fade',
            ),
        );
    }

    $form['save_draft'] = array(
        '#type' => 'submit',
        '#value' => t('Save/Edit'),
        '#submit' => array('_addresses_submit'),
    );
    return $form;
}

function _load_addresses($form, &$form_state) {
    return $form['em_addresses'];
}

function _eligibility($form, &$form_state = NULL) {
    $form = array();
    $form['title'] = array(
        '#markup' => t("<h2>Eligibility</h2>"));
    $unions = returnUnions();
    $form_key = "em_unions";

    //Set count of fields to show
    $form_state['storage'][$form_key] = isset($form_state['storage'][$form_key]) ? $form_state['storage'][$form_key] : 0;


    if (isset($form_state['triggering_element']) && $form_state['triggering_element']['#name'] == "Remove Union" && $form_state['storage'][$form_key] > 0) {
        $form_state['storage'][$form_key] --;
    } else if (isset($form_state['triggering_element']) && $form_state['triggering_element']['#name'] == "Add Union") {
        $form_state['storage'][$form_key] ++;
    } else if ($form_state['storage'][$form_key] == 0) {
        $form_state['storage'][$form_key] = 1;
    }

    $form[$form_key] = array(
        '#type' => 'container',
        '#tree' => TRUE,
        '#prefix' => '<div id="' . $form_key . '">',
        '#suffix' => '</div>',
    );

    if ($form_state['storage'][$form_key]) {
        for ($i = 0; $i < $form_state['storage'][$form_key]; $i++) {
            $form[$form_key][$i] = array(
                '#type' => 'fieldset',
                '#title' => t('Union'),
                '#tree' => TRUE,
            );

            $form[$form_key][$i]["union_id"] = array(
                '#type' => 'select',
                '#title' => t('Union'),
                '#options' => $unions,
                '#required' => TRUE,
            );
            $form[$form_key][$i]["union_no"] = array(
                '#type' => 'textfield',
                '#title' => t('Union #'),
                '#required' => TRUE,
            );
        }
    }

    $form[$form_key]['add_union'] = array(
        '#type' => 'button',
        '#href' => '',
        '#title' => t('Add Union'),
        '#default_value' => t('Add Union'),
        '#name' => t('Add Union'),
        '#limit_validation_errors' => array(),
        '#executes_submit_callback' => FALSE,
        '#ajax' => array(
            'callback' => '_load_unions',
            'wrapper' => $form_key,
            'effect' => 'fade',
        ),
    );
    if ($form_state['storage'][$form_key] > 1) {
        $form[$form_key]['remove_union'] = array(
            '#type' => 'button',
            '#href' => '',
            '#title' => t('Remove Union'),
            '#default_value' => t('Remove Union'),
            '#name' => t('Remove Union'),
            '#limit_validation_errors' => array(),
            '#executes_submit_callback' => FALSE,
            '#ajax' => array(
                'callback' => '_load_unions',
                'wrapper' => $form_key,
                'effect' => 'fade',
            ),
        );
    }

    $form['agency'] = array(
        '#type' => 'textfield',
        '#title' => t('Agency'),
        '#default_value' => '',
        '#required' => TRUE,
    );
    $form['agent_name'] = array(
        '#type' => 'textfield',
        '#title' => t('Agent Name'),
        '#default_value' => '',
        '#required' => TRUE,
    );
    $form['agent_phone'] = array(
        '#type' => 'textfield',
        '#title' => t('Agent Phone'),
        '#default_value' => '',
        '#required' => TRUE,
    );
    $form['file'] = array(
        '#type' => 'managed_file',
        '#title' => t('Upload non-union docs'),
        '#required' => FALSE,
            //  '#upload_location' => 'public://',
    );
    $form['save_draft'] = array(
        '#type' => 'submit',
        '#value' => t('Save Draft'),
        '#submit' => array('_register_em_eligibility_submit'),
    );
    return $form;
}

function _load_unions($form, &$form_state = NULL) {
    return $form['em_unions'];
}

/* FORM STEP : EDUCATION INFO */

function _education($form, &$form_state = NULL) {
    $form = array();
    $form['title'] = array(
        '#markup' => t("<h2>Education</h2>")); 
    $form_key = "em_schools";

    $states = getStates();

    //Set no of schools to display
    $form_state['storage'][$form_key] = isset($form_state['storage'][$form_key]) ? $form_state['storage'][$form_key] : 0;
    if (isset($form_state['triggering_element']) && $form_state['triggering_element']['#name'] == "Remove School" && $form_state['storage'][$form_key] > 0) {
        $form_state['storage'][$form_key] --;
    } else {
        $form_state['storage'][$form_key] ++;
    }

    $form[$form_key] = array(
        '#type' => 'container',
        '#tree' => TRUE,
        '#prefix' => '<div id="' . $form_key . '">',
        '#suffix' => '</div>',
    );

    for ($i = 0; $i < $form_state['storage'][$form_key]; $i++) {
        $form[$form_key][$i] = array(
            '#type' => 'fieldset',
            '#title' => t('School'),
            '#tree' => TRUE,
        );

        $form[$form_key][$i]['school_name'] = array(
            '#type' => 'textfield',
            '#title' => t('Name of school'),
            '#default_value' => '',
            '#required' => TRUE,
        );
        $form[$form_key][$i]['school_city'] = array(
            '#type' => 'textfield',
            '#title' => t('City'),
            '#default_value' => '',
            '#required' => TRUE,
        );
        $form[$form_key][$i]['school_state'] = array(
            '#type' => 'select',
            '#title' => t('State'),
            '#options' => $states,
            '#default_value' => 'CA',
            '#required' => TRUE,
        );
        $form[$form_key][$i]['school_zip'] = array(
            '#type' => 'textfield',
            '#title' => t('Zip'),
            '#default_value' => '',
            '#required' => TRUE,
            '#element_validate' => array('element_validate_integer_positive'),
        );
        $form[$form_key][$i]['school_type'] = array(
            '#type' => 'select',
            '#title' => t('Type'),
            '#options' => array('' => 'Select', 'php' => t('Type 1'), 'css' => t('Type 2'),),
            '#default_value' => array(),
            '#required' => TRUE,
        );
        $form[$form_key][$i]['school_years'] = array(
            '#type' => 'textfield',
            '#title' => t('Years at school'),
            '#default_value' => '',
            '#required' => TRUE,
            '#element_validate' => array('element_validate_integer_positive'),
        );
        $form[$form_key][$i]['school_grade_level'] = array(
            '#type' => 'textfield',
            '#title' => t('Grade Level'),
            '#default_value' => '',
            '#required' => TRUE,
        );
        $form[$form_key][$i]['has_graduated'] = array(
            '#type' => 'radios',
            '#title' => t('Graduated'),
            '#options' => array(1 => t('Yes'), 0 => t('No')),
            '#default_value' => array(),
            '#required' => TRUE,
        );
    }

    $form[$form_key]['add_school'] = array(
        '#type' => 'button',
        '#href' => '',
        '#title' => t('Add School'),
        '#default_value' => t('Add School'),
        '#name' => t('Add School'),
        '#limit_validation_errors' => array(),
        '#executes_submit_callback' => FALSE,
        '#ajax' => array(
            'callback' => '_add_school',
            'wrapper' => $form_key,
            'effect' => 'fade',
        ),
    );
    if ($form_state['storage'][$form_key] > 1) {
        $form[$form_key]['remove_school'] = array(
            '#type' => 'button',
            '#href' => '',
            '#title' => t('Remove School'),
            '#default_value' => t('Remove School'),
            '#name' => t('Remove School'),
            '#limit_validation_errors' => array(),
            '#executes_submit_callback' => FALSE,
            '#ajax' => array(
                'callback' => '_add_school',
                'wrapper' => $form_key,
                'effect' => 'fade',
            ),
        );
    }
    $form['save_draft'] = array(
        '#type' => 'submit',
        '#value' => t('Save/Edit'),
        '#submit' => array('_register_em_education_submit'),
    );
    return $form;
}

function _add_school($form, &$form_state = NULL) {
    return $form['em_schools'];
}

/* FORM STEP : Waivers AND RELEASES INF0 */

function _waivers_releases($form, &$form_state = NULL) {
    $form = array();
    $form['title'] = array(
        '#markup' => t("<h2>Waivers and Releases</h2>"));
    $form_key = "em_waivers_releases";

    $states = getStates();

    $form['code_of_conduct'] = array(
        '#type' => 'checkbox',
        '#title' => t('Code of Conduct'),
        '#default_value' => "",
        '#required' => TRUE,
    );
    $form['photo_release'] = array(
        '#type' => 'checkbox',
        '#title' => t('Photo Release'),
        '#default_value' => '',
        '#required' => TRUE,
    );
    $form['video_release'] = array(
        '#type' => 'checkbox',
        '#title' => t('Video Release'),
        '#default_value' => '',
        '#required' => TRUE,
    );
    $form['emergency_contact'] = array(
        '#type' => 'fieldset',
        '#title' => t('Emergency Contact'),
        '#tree' => TRUE,
    );
    $form['emergency_contact']['emergency_contact_first'] = array(
        '#type' => 'textfield',
        '#title' => t('First Name'),
        '#default_value' => '',
        '#required' => TRUE,
    );
    $form['emergency_contact']['emergency_contact_last'] = array(
        '#type' => 'textfield',
        '#title' => t('Last Name'),
        '#default_value' => '',
        '#required' => TRUE,
    );
    $form['emergency_contact']['emergency_phone'] = array(
        '#type' => 'textfield',
        '#title' => t('Phone'),
        '#default_value' => '',
        '#required' => TRUE,
    );
    $form['emergency_contact']['emergency_address'] = array(
        '#type' => 'textfield',
        '#title' => t('Address'),
        '#default_value' => '',
        '#required' => TRUE,
    );
    $form['emergency_contact']['emergency_apt'] = array(
        '#type' => 'textfield',
        '#title' => t('APT'),
        '#default_value' => '',
        '#required' => TRUE,
    );
    $form['emergency_contact']['emergency_city'] = array(
        '#type' => 'textfield',
        '#title' => t('City'),
        '#default_value' => '',
        '#required' => TRUE,
    );
    $form['emergency_contact']['emergency_state'] = array(
        '#type' => 'select',
        '#title' => t('State'),
        '#options' => $states,
        '#default_value' => 'CA',
        '#required' => TRUE,
    );
    $form['emergency_contact']['emergency_zip'] = array(
        '#type' => 'textfield',
        '#title' => t('Zip'),
        '#default_value' => '',
        '#required' => TRUE,
    );
    $form['emergency_contact']['relationship_to_participant'] = array(
        '#type' => 'textfield',
        '#title' => t('Relationship to Participant'),
        '#default_value' => '',
        '#required' => TRUE,
    );
    $form['name_of_insurance'] = array(
        '#type' => 'textfield',
        '#title' => t('Name of Insurance'),
        '#default_value' => '',
        '#required' => TRUE,
    );
    $form['medical_insurance_no'] = array(
        '#type' => 'textfield',
        '#title' => t('Medical Insurance#'),
        '#default_value' => '',
        '#required' => TRUE,
    );
    $form['disabilities'] = array(
        '#type' => 'radios',
        '#options' => array(1 => t('Yes'), 0 => t('No')),
        '#title' => t('Disabilities, Ailments, Needs, Treatments? '),
        '#default_value' => array(),
        '#required' => TRUE,
    );
    $form['disability_explanation'] = array(
        '#type' => 'textarea',
        '#title' => t('Disability Explanation'),
        '#default_value' => '',
        '#required' => FALSE,
    );
    $form['medication_allergy'] = array(
        '#type' => 'radios',
        '#options' => array(1 => t('Yes'), 0 => t('No')),
        '#title' => t('Allergic to any medication or anything else?'),
        '#default_value' => array(),
        '#required' => TRUE,
    );
    $form['allergy_explanation'] = array(
        '#type' => 'textarea',
        '#title' => t('Allergy Explanation'),
        '#default_value' => '',
        '#required' => FALSE,
    );
    $form['taking_medication'] = array(
        '#type' => 'radios',
        '#options' => array(1 => t('Yes'), 0 => t('No')),
        '#title' => t('Taking any medication?'),
        '#default_value' => array(),
        '#required' => TRUE,
    );
    $form['medication_explanation'] = array(
        '#type' => 'textarea',
        '#title' => t('Medication Explanation'),
        '#default_value' => '',
        '#required' => FALSE,
    );
    $form['digital_signature'] = array(
        '#type' => 'textfield',
        '#title' => t('Digital Signature'),
        '#default_value' => '',
        '#required' => TRUE,
    );
    $form['digital_date_stamp'] = array(
        '#type' => 'textfield',
        '#title' => t('Digital Date Stamp'),
        '#default_value' => format_date(time(), 'custom', 'l j F Y'),
        '#required' => TRUE,
    );


    $form['save_draft'] = array(
        '#type' => 'submit',
        '#value' => t('Save/Edit'),
        '#submit' => array('_register_em_waivers_releases_submit'),
    );
    return $form;
}

/* FORM STEP : ADDITIONAL INF0 */

function _additional_info($form, &$form_state = NULL) {
    $logged_in_user = getCurrentUser();
    $la_user_table = db_select('la_users', 'lu')
            ->fields('lu')
            ->condition('user_id', $logged_in_user->uid, '=')
            ->execute()
            ->fetchAssoc();
    $form = array();
    $form['title'] = array(
        '#markup' => t("<h2>Additional Information</h2>"));

    $form['more_info'] = array(
        '#type' => 'radios',
        '#title' => t('Would you like for staff to contact you with more info on the Looking Ahead Program?'),
        '#options' => array(1 => 'Yes', 0 => t('No')),
        '#default_value' => $la_user_table['can_staff_connect'],
        '#required' => FALSE,
    );
    $form['education_counseling'] = array(
        '#type' => 'radios',
        '#title' => t('Are you interested in a phone or in-person appt for Educational Counseling?'),
        '#options' => array(1 => 'Yes', 0 => t('No')),
        '#default_value' => $la_user_table['educational_counseling'],
        '#required' => FALSE,
    );
    $form['family_counseling'] = array(
        '#type' => 'radios',
        '#title' => t('Are you interested in a phone or in-person appt for Family/Individual Counseling?'),
        '#options' => array(1 => 'Yes', 0 => t('No')),
        '#default_value' => $la_user_table['family_individual_counseling'],
        '#required' => FALSE,
    );
    $form['heard_from'] = array(
        '#type' => 'checkboxes',
        '#title' => t('How did you hear about us?'),
        '#options' => array('Google' => t('Google'), 'Friend' => t('Friend')),
        '#default_value' => array(),
        '#required' => FALSE,
    );
    $form['save_draft'] = array(
        '#type' => 'submit',
        '#value' => t('Save Draft'),
        '#submit' => array('_register_em_additional_info_submit'),
    );
    return $form;
}
